package com.wuwei.tms.upload;

import com.wuwei.tms.pojo.Tool;
import com.wuwei.tms.utils.token.TokenUtil;
import com.wuwei.tms.workbench.mapper.ToolMapper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Component
@Transactional
public class DataDAO<T> {

    @Autowired
    private HttpServletRequest request;

    @Resource
    private ToolMapper toolMapper;

    public void save(List<T> cachedDataList) {
        String token = request.getHeader("Authorization");
        Long empno = TokenUtil.getEmpnoFromToken(token);
        for (int i = 0; i < cachedDataList.size(); i++) {
            T a = cachedDataList.get(i);
            if (a instanceof Tool) {
                ((Tool) a).setUserableNum(((Tool) a).getAllNumber());
                ((Tool) a).setCreateTime(LocalDateTime.now());
                ((Tool) a).setCreateBy(empno);
                toolMapper.insert((Tool)a);
            }
        }
    }
}
